<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Exception;
use think\Request;
use app\admin\model\Permission as PermissionModel;
use app\admin\model\RolePermission as RolePermissionModel;

class Permission extends Controller
{
    /**
     * 指定当前数据表
     * @var string
     */
    public $table = 'permission';

    /**
     * 权限管理视图
     * @return mixed
     */
    public function index() {

        return $this->fetch();
    }

    /**
     * 所有数据
     * @return \think\response\Json
     */
    public function getList() {
        //$role = new RoleModel();
        // 分页查询
        $result = DB::table($this->table)->order('sort', 'asc')->select();
        return json($result);
    }

    /**
     * 添加权限
     * @param Request $request
     * @return \think\response\Json
     */
    public function addPermission(Request $request) {
        // 获取参数仅包含数组中字段
        $data = $request->only(['pid', 'name', 'type', 'sort', 'url', 'icon', 'description', 'status']);
        if ($data['type'] == 0) {
            $data['pid'] = 0;
        }
        // 添加数据
        $result = PermissionModel::create($data);
        // 返回数据
        if ($result) {
            return json(['success'=>true, "msg"=>'添加成功']);
        } else {
            return json(['success'=>false, "msg"=>'添加失败']);
        }
    }

    /**
     * 修改权限
     * @param Request $request
     * @return \think\response\Json
     */
    public function updatePermission(Request $request) {
        // 获取参数仅包含数组中字段
        $data = $request->only(['id', 'pid', 'name', 'type', 'sort', 'url', 'icon', 'description', 'status']);
        if ($data['type'] == 0) {
            $data['pid'] = 0;
        }
        // 更新数据
        $result = PermissionModel::update($data);
        // 返回数据
        if ($result) {
            return json(['success'=>true, "msg"=>'修改成功']);
        } else {
            return json(['success'=>false, "msg"=>'修改失败']);
        }
    }

    public function delete() {
        $id = input('id');
        // 删除数据
        $result = PermissionModel::destroy($id);
        // 返回数据
        if ($result) {
            return json(['success'=>true, "msg"=>'删除成功']);
        } else {
            return json(['success'=>false, "msg"=>'删除权限不存在']);
        }
    }

    /**
     * 传入ID 查询权限
     * @param $id
     * @return \think\response\Json
     */
    public function getPermissionById($id) {
        $result = PermissionModel::get($id);
        return json($result);
    }

    /**
     * 查询按钮类型
     * @param $id
     * @return \think\response\Json
     */
    public function getTypePermission($type) {
        $permission = new PermissionModel();
        $result = $permission->where('type', $type)->select();
        return json($result);
    }

    /**
     * 传入角色ID Ztree展示
     * @param $roleId
     * @return \think\response\Json
     */
    public function getPermissionZTreeNodes($roleId) {
        $sql = 'SELECT
                p1.id,
                p1.pid,
                p1.name,
                (
                    "TRUE"
                ) as open,
                (
                    CASE
                        WHEN (p2.pid IS NULL)  THEN "FALSE" ELSE "TRUE"
                    END
                ) as checked
            FROM
                permission AS p1
            LEFT JOIN role_permission AS p2 ON p2.rid = ?
            AND p1.id = p2.pid';
        $result = DB::query($sql, [$roleId]);
        return json($result);
    }

    /**
     * 分配权限操作
     * @param Request $request
     * @return \think\response\Json
     */
    public function modifyRolePermission(Request $request) {
        $data = $request->param();
        //事物
        $rolePermission = new RolePermissionModel();
        $rolePermission->startTrans();
        try {
            //删除关联表数据
            $rolePermission->where("rid", $data[0]['rid'])->delete();
            $rolePermission->saveAll($data);
        } catch (Exception $e) {
            $rolePermission->rollback();
            return json(['success'=>false, "msg"=>'分配失败']);
        }
        $rolePermission->commit();
        return json(['success'=>true, "msg"=>'分配成功']);
    }

}